// pages/activity/detail/activitydetail.js
var config = require('../../../config.js');
var request = require('../../../utils/request.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activtyId: '',
    detailInfo: {},
    goodsSlide: [],
    goodsAttrs: [],
    goodsDesc: [],
    btnStr: '立即购买',
    shareNum: 0,//分享次数
    isIpx: app.globalData.isIpx,
    showHome: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      activtyId: options.id
    });
    let url = config.activityDetailUrl;
    request.requestPost(url, { id: options.id })
      .then(data => {

        var slide = data.productImg;
        var slides = slide.split(',');

        var attr = data.productAttr;
        var attrs = attr.split(';');

        this.setData({
          detailInfo: data,
          goodsSlide: slides,
          goodsAttrs: attrs,
          goodsDesc: JSON.parse(data.detail),
        })
      }).catch(msg => {
        console.log(msg);
      });
    this.getShareInfo();
    this.setData({
      showHome: app.globalData.showHome
    });

    wx.showShareMenu({
      withShareTicket: true
    })

  },

  getShareInfo: function () {
    var numstr = wx.getStorageSync('share' + this.data.activtyId) || 0;
    if (numstr < 3) {
      this.setData({
        shareNum: parseInt(numstr),
        btnStr: '分享' + (3 - numstr) + '个微信群后立即购买'
      })
    } else {
      this.setData({
        shareNum: 3,
        btnStr: '立即购买'
      });
    }
  },


  setShareNum: function (num) {
    wx.setStorageSync('share' + this.data.activtyId, num);
  },

  /**
   * 立即购买
   */
  buyAction: function () {
    var numstr = wx.getStorageSync('share' + this.data.activtyId);
    if (numstr && parseInt(numstr) == 3) {
      let id = this.data.activtyId;
      wx.navigateTo({
        url: '../../settle/settle?activityid=' + id,
      })
    } else {
      //转发
      this.onShareAppMessage({ from: 'button', target: '' });
    }
  },

  /**
   * 转发
   */
  onShareAppMessage: function (option) {
    if (option.from === 'button') {
      // 来自页面内转发按钮
      console.log(option.target)
    }
    var self = this;
    return {
      title: this.data.detailInfo.activityName,
      path: 'pages/activity/detail/activitydetail?id=' + this.data.activtyId,
      imageUrl: this.data.goodsSlide[0],
      success: function (res) {
        // 转发成功
        var shareTickets = res.shareTickets;
        console.log(shareTickets);
        if (shareTickets == undefined || shareTickets.length == 0) {
          wx.showToast({
            title: '请转发到微信群',
            icon: 'none'
          })
          return false;
        }
        wx.getShareInfo({
          shareTicket: shareTickets[0],
          success: function (res) {
            console.log(res);
            self.decodeEncryptedData(res);
          }
        })

      },
      fail: function (res) {
        // 转发失败
      }
    }
  },


  decodeEncryptedData: function (data) {
    let self = this;
    var url = config.shareDecodeUrl;
    const appId = app.globalData.appId;
    const sessionKey = wx.getStorageSync('sessionKey');
    const iv = data.iv;
    const encryptedData = data.encryptedData;
    request.requestPost(url, { appId: appId, sessionKey: sessionKey, iv: iv, encryptedData: encryptedData }).then(data => {
      console.log(data);
      let openGId = data.openGId;
      self.setShareGuid(openGId);
    }).catch(msg => {
      console.log(msg);
    });
  },


  setShareGuid: function (guid) {
    var oldguid = wx.getStorageSync('shareguid' + this.data.activtyId);
    console.log(oldguid);
    var oldguids = oldguid.split(';');
    var hasSamle = false;
    oldguids.map(item => {
      if (item == guid) {
        hasSamle = true;
        wx.showModal({
          title: '提醒',
          content: '不能分享到同一个微信群',
        });
      }
    });

    if (!hasSamle) {
      let num = this.data.shareNum + 1;
      wx.setStorageSync('share' + this.data.activtyId, num);

      wx.setStorageSync('shareguid' + this.data.activtyId, oldguid + ";" + guid)
    }
    this.getShareInfo();
  },


  goToCar:function(){
    wx.switchTab({
      url: '../../index/index',
    })
  },



})